package com.xsky.logic.user;

import java.util.List;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.DateConverter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.xsky.database.dao.impl.BaseEntityDaoImpl;
import com.xsky.operation.vo.Staff;


public class UserLogic {

	public String resetPwd(String userId){
		
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		Staff userEntity = new Staff(); 
		userEntity = (Staff) baseDao.findByPrimaryKey("Staff", "id", userId);
		userEntity.setPassword("000000");
		baseDao.updateEntity(userEntity);
		return "重置密码成功！";
	}
	public String changePassword(String id, String newPassword)
	{
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		Staff entity = new Staff();
		entity = (Staff)baseDao.findByPrimaryKey("Staff", "id", id);
		entity.setPassword(newPassword);
		baseDao.updateEntity(entity);
		return entity.getPassword();
	}
	public String logon(String userName, String password)
	{
		String dataXml = "";
		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
		Staff userEntity = new Staff(); 
		List userList = baseDao.findByHql("from Staff model where model.userName='"+userName+"' and model.password='"+password+"'");
		if(userList!=null&&userList.size()>0)
		{
			userEntity = (Staff)userList.get(0);
			XStream xs = new XStream(new DomDriver());
			String defaultFormat = "yyyy-MM-dd HH:mm:ss";
			String[] acceptableFormat = {"yyyy-MM-dd HH:mm:ss","yyyy-MM-dd"};
			xs.registerConverter(new DateConverter(defaultFormat,acceptableFormat));
			xs.alias("item", Staff.class);
			dataXml=xs.toXML(userEntity);
		}
		return dataXml.equals("")?null:dataXml;
	}
	public static void main(String[] args) {
		System.out.println(new UserLogic().logon("congpy", "308333"));
	}
//	public void inputTxl(String filePath) throws IOException
//	{
//		List entityList = new ArrayList();
//		File file=new File(filePath);
//		if(!file.exists()||file.isDirectory())
//			throw new FileNotFoundException();
//		BufferedReader br=new BufferedReader(new FileReader(file));
//		String temp=null;
//		temp=br.readLine();
//		while(temp!=null)
//		{
//			String realName ="";
//			String phone = "";
//			String mobile = "";
//			String email = "";
//			String post = "";
//			int length = temp.split(",").length;
//			
//			for(int i = 0; i < length; i++)
//			{
//				switch(i)
//				{
//					case 0:realName =temp.split(",")[0];break;
//					case 1:phone = temp.split(",")[1];break;
//					case 2:mobile = temp.split(",")[2];break;
//					case 3:email = temp.split(",")[3];break;
//					case 4:post = temp.split(",")[4];break;
//				}
//			}
//			TabAddressBook entity = new TabAddressBook();
//			if(realName==null||"".equals(realName))
//			{
//				entity = (TabAddressBook)entityList.get(entityList.size()-1);
//				if(entity==null)
//				{
//					entity = new TabAddressBook();
//				}
//				if(phone!=null&&!"".equals(phone))
//				{
//				
//					entity.setPhone(entity.getPhone()+","+phone);
//				}
//				if(mobile!=null&&!"".equals(mobile))
//				{
//					entity.setMobilePhone(entity.getMobilePhone()+","+mobile);
//				}
//				if(email!=null&&!"".equals(email))
//				{
//					entity.setEmail(entity.getEmail()+","+email);
//				}
//				if(post!=null&&!"".equals(post))
//				{
//					entity.setPositionName(entity.getPositionName()+","+post);
//				}
//			}
//			else
//			{
//				entity = new TabAddressBook();
//				entity.setId(UUID.randomUUID().toString());
//				entity.setContactName(realName);
//				if(phone!=null&&!"".equals(phone))
//				{
//				
//					entity.setPhone(phone);
//				}
//				if(mobile!=null&&!"".equals(mobile))
//				{
//					entity.setMobilePhone(mobile);
//				}
//				if(email!=null&&!"".equals(email))
//				{
//					entity.setEmail(email);
//				}
//				if(post!=null&&!"".equals(post))
//				{
//					entity.setPositionName(post);
//				}
//				entityList.add(entity);
//			}
//			temp=br.readLine();	
//		}
////		for(int i = 0; i < entityList.size(); i++)
////		{
////			TabUser user = (TabUser)entityList.get(i);
////			System.out.println(user.getRealName()+" "+user.getPhone()+" "+user.getMobile()+" "+user.getEmail()+" "+user.getPost());
////		}
//		BaseEntityDaoImpl baseDao = new BaseEntityDaoImpl();
//		baseDao.insertEntity(entityList);
//
//	}
}
